//
//  GoodsDicItem.swift
//  ReactNativeIOS
//
//  Created by FishWithoutWings on 2017/12/16.
//  Copyright © 2017年 chenliang. All rights reserved.
//

import RealmSwift

class GoodsDicItem: Object {
    
    @objc dynamic var id = 0
    @objc dynamic var dicId = 0
    @objc dynamic var itemValue = ""
    @objc dynamic var sortNo = 0
    
    override class func primaryKey() -> String? {
        return "id"
    }
    
    class func create(_ goodsDicItems: [Any], isAsync: Bool = true) {
        let closure = {
            let realm = YYRealm.initialize()
            
            realm.beginWrite()
            realm.delete(realm.objects(self))
            for goodsDicItem in goodsDicItems {
                realm.create(self, value: goodsDicItem, update: true)
            }
            try! realm.commitWrite()
        }
        
        if isAsync {
            YYRealm.async(closure)
        } else {
            closure()
        }
    }
    
    class func getAll(dicId: Int) -> [GoodsDicItem] {
        let realm = YYRealm.initialize()
        return realm.objects(self).filter("dicId = \(dicId)").map({ (goodsDicItem) -> GoodsDicItem in
            return goodsDicItem
        })
    }

}
